<?php defined('SYSPATH') or die('No direct script access.') ;
/**
 * 页面消息反馈助手
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: message.php 213 2012-05-24 09:33:22Z yellow.chow $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Helper_Message extends Helper
{

    /**
     * 视图目录
     *
     * @var string
     */
    public static $directory = '';

    /**
     * 消息标题
     *
     * @var string
     */
    protected $_title = '';

    /**
     * 提示消息
     *
     * @var string
     */
    protected $_message = '';

    /**
     * 跳转链接
     *
     * @var array
     */
    protected $_links = array();

    /**
     * 是否自动跳转
     *
     * @var bool
     */
    protected $_auto_redirect = TRUE;

    /**
     * 跳转前停留时间
     *
     * @var int
     */
    protected $_wait_time = 5;

    /**
     * 构造方法
     */
    public function __construct($message = NULL, $title = NULL)
    {
        $message AND $this->message($message);
        $title   AND $this->title($title);
    }

    /**
     * 设置消息标题
     *
     * @param  string  $title
     * @param  array   $values
     * @return Helper_Feedback
     */
    public function title($title, array $values = NULL)
    {
        $this->_title = __($title, $values);

        return $this;
    }

    /**
     * 设置消息内容
     *
     * @param  string  $message
     * @param  array   $values
     * @return Helper_Feedback
     */
    public function message($message, array $values = NULL)
    {
        $this->_message = __($message, $values);

        return $this;
    }

    /**
     * 添加跳转链接
     *
     * @param  string  $url
     * @param  string  $text
     * @param  string  $target
     * @return Helper_Feedback
     */
    public function link($url, $text, $target = '_self')
    {
        $this->_links[] = array(
            'url'    => $url,
            'text'   => __($text),
            'target' => $target,
        );

        return $this;
    }

    /**
     * 设置是否自动跳转及跳转前停留时间
     *
     * @param  bool  $auto_redirect
     * @param  int   $wait_time
     * @return Helper_Feedback
     */
    public function redirect($auto_redirect = TRUE, $wait_time = 5)
    {
        $this->_auto_redirect = (bool) $auto_redirect;
        $this->_wait_time = (int) $wait_time;

        return $this;
    }

    /**
     * 立即渲染输出视图并退出
     *
     * @parma  string  $type  消息类型 (info|error|success|waring|confirm)
     * @param  string  $file
     */
    public function render($type = 'error', $file = 'feedback')
    {
        $view = View::factory(self::$directory.$file);

        if (empty($this->_title))
        {
            $this->_title = __('feedback.'.$type);
        }

        $view->set(array(
            'type'          => $type,
            'title'         => $this->_title,
            'message'       => $this->_message,
            'links'         => $this->_links,
            'auto_redirect' => $this->_auto_redirect,
            'wait_time'     => $this->_wait_time,
        ));

        // 输出反馈信息，并中断执行
        exit($view->render());
    }

}